package com.aster.service.manage.service.impl;

import com.aster.common.base.BaseServiceImpl;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.PageUtils;
import com.aster.service.manage.dao.EscortUserDutyDao;
import com.aster.service.manage.domain.TbEnterpriseProfitRatioInfo;
import com.aster.service.manage.domain.TbEscortEnterpriseInfo;
import com.aster.service.manage.domain.TbEscortUserDuty;
import com.aster.service.manage.dto.DtoEscortEnterpriseInfo;
import com.aster.service.manage.dto.DtoEscortUserDuty;
import com.aster.service.manage.dto.do2dto.EscortEnterpriseInfoConvert;
import com.aster.service.manage.dto.do2dto.EscortUserDutyConvert;
import com.aster.service.manage.service.EscortUserDutyService;
import com.aster.service.manage.service.criteria.EscortUserDutyCriteria;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortUserDutyService<br>
 */
@Service("escortUserDutyService")
public class  EscortUserDutyServiceImpl  extends BaseServiceImpl implements EscortUserDutyService {
  private final static Logger log= Logger.getLogger(EscortUserDutyServiceImpl.class);
	

	@Autowired
    private EscortUserDutyDao dao;

		
	public EscortUserDutyDao getDao() {
		return dao;
	}


	@Override
	public List<TbEscortUserDuty> getEscortUserDutyList(TbEscortUserDuty tbEscortUserDuty) {
		List<TbEscortUserDuty> list = dao.getList(tbEscortUserDuty);
		return list;
	}

	@Override
	public void editDutyState(TbEscortUserDuty tbEscortUserDuty, UserToken userToken) throws ServiceException {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(tbEscortUserDuty.getDutyTime());
			calendar.set(Calendar.HOUR, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			Date date = calendar.getTime();
			tbEscortUserDuty.setDutyTime(date);

			List<TbEscortUserDuty> list = dao.queryByIdAndDutyTime(tbEscortUserDuty);

			if (list != null && list.size() > 0){
				TbEscortUserDuty duty = list.get(0);
				duty.setDutyState(tbEscortUserDuty.getDutyState());
				dao.update(duty);
			}else {
				tbEscortUserDuty.setEnterpriseId(userToken.getEnterpriseId());
				dao.add(tbEscortUserDuty);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<TbEscortUserDuty> queryByIdAndDutyTime(TbEscortUserDuty tbEscortUserDuty) {
		return dao.queryByIdAndDutyTime(tbEscortUserDuty);
	}
}
